<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Home extends MX_Controller
{

    private $_langCode;

    function __construct()
    {
        parent::__construct();
        $this->load->library('DefaultLayout');
        $this->load->library('Minifier');
        $this->_langCode = $this->mycommon->getLangCode();
        $this->load->language('common', $this->_langCode);
    }

    public function lang($_lang)
    {
        $valid = array('vn', 'jp', 'en');
        $urlDirect = '';
        if (in_array($_lang, $valid) !== FALSE) {
            $langCode = $this->mycommon->decodeLang($_lang);
            $urlEncode = $this->input->get('direct', true);
            $urlDirect = urldecode($urlEncode);
            $this->mycommon->setLangCode($langCode);
        }
        redirect($urlDirect);
    }

    public function cat($catId, $page = '')
    {
        $catId = (int) $catId;
        $page = ($page != '') ? intval($page) : 1;
        if ($catId != 0) {
            $data = array();
            $this->load->library('Breadcrumb');
            $this->load->model('home/home_model');
            $langCode = $this->mycommon->fetchLangCode($this->_langCode);
            $allProduct = $this->home_model->getProductByCatId($catId, $langCode);
            $catInfo = $this->home_model->getCatInfo($catId, $langCode);
            $this->load->library('pagination');
            $numPost = $this->home_model->getNumProByCatId($catId, $langCode);
            $config['base_url'] = site_url("home/cat/$catId");
            $config['total_rows'] = $numPost;
            $config['per_page'] = 8;
            $config['use_page_numbers'] = TRUE;
            $config['first_link'] = '&lsaquo;-';
            $config['last_link'] = '-&lsaquo;';
            $config['cur_tag_open'] = "<a href='#'>";
            $config['cur_tag_close'] = "</a>";
            $config['cur_page'] = $page;
            $config['uri_segment'] = 4;
            $this->pagination->initialize($config);
            $start = ($page - 1) * $config['per_page'];
            $data['pagination'] = $this->pagination->create_links();
            $data['cur_page'] = $page;
            $data['total_page'] = $this->pagination->num_links;
            $parentCatInfo = $this->home_model->getCatInfo($catInfo[0]['parent_id'], $langCode);
            $data['allProduct'] = $allProduct;
            $this->breadcrumb->addCrumb($parentCatInfo[0]['name']);
            $this->breadcrumb->addCrumb($catInfo[0]['name'], site_url('home/cat/' + $catId));
            $data['breadcrumb'] = $this->breadcrumb->makeBread();
            $headInfo = array(
                'head_title' => 'Hitachi - ' . $catInfo[0]['name'],
            );
            $this->minifier->addJs('jquery.tooltip');
            $this->minifier->addCss('jquery.tooltip');
            $this->minifier->addCss('product/style');
            $this->defaultlayout->setHeadInfo($headInfo);
            $this->defaultlayout->_loadHeader();
            $this->load->view('home/cat_view', $data);
            $this->defaultlayout->_loadFooter();
        } else {
            redirect();
        }
    }

}

?>
